home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / pctchnqs / 1990 / number3 / clone.c < prev    next >
Text File  |  1990-06-12  |  2KB  |  83 lines

  1. /* CLONE.C -- Clone Directories with TDRF, by Tom Swan */
  2.  
  3. #include <stdio.h>
  4. #include <dir.h>
  5. #include <dos.h>
  6. #include <string.h>
  7.  
  8. #define PATHLEN 65
  9.  
  10. const char *batchFile = "c:\\clone@@@.bat";
  11. char originalPath[PATHLEN];
  12. FILE *bf;        /* Batch file */
  13.  
  14. void readDirectory(void);
  15. void preparePath(void);
  16.  
  17. main(int argc, char *argv[])
  18. {
  19.    int err = 0;
  20.  
  21.    if (getcwd(originalPath, PATHLEN) != NULL) {
  22.       if (argc > 1) err = chdir(*++argv);
  23.       if (!err) {
  24.          if ((bf = fopen(batchFile, "wt")) == NULL) {
  25.             fprintf(stderr, "Error creating batch file\n");
  26.             return(1);
  27.          }
  28.          readDirectory();    /* Write commands to batch file */
  29.          fclose(bf);         /* Close batch file before running */
  30.          execl(getenv("COMSPEC"), argv[0], "/C", batchFile, NULL);
  31.          remove(batchFile);  /* Erase the batch file */
  32.          chdir(originalPath);
  33.       } else {
  34.          puts("Clone Directories with TDRF, by Tom Swan");
  35.          puts("Syntax: CLONE [-?] [pathName]");
  36.       }
  37.    }
  38.    return(0);   /* End program, no errors */
  39. }
  40.  
  41. /*--- Walk through all paths from current directory */
  42. void readDirectory()
  43. {
  44.    int done;
  45.    struct ffblk sr;
  46.  
  47.    preparePath();
  48.    done = findfirst("*.*", &sr, FA_DIREC);
  49.    while (!done) {
  50.       if ((sr.ff_name[0] != '.') &&
  51.           (sr.ff_attrib & FA_DIREC)) {
  52.          chdir(sr.ff_name);   /* Change to next level */
  53.          readDirectory();     /* Process files there */
  54.          chdir("..");         /* Return to previous level */
  55.       }
  56.       done = findnext(&sr);
  57.    }
  58. } /* readDirectory */
  59.  
  60. /*--- Write TDRF commands to batch file bf */
  61. void preparePath()
  62. {
  63.    char *p;                /* Next backslash position */
  64.    char fExpand[PATHLEN];  /* Full current path name  */
  65.    char *plainPath;        /* Path without drive info */
  66.  
  67.    plainPath = getcwd(fExpand, PATHLEN) + 2;
  68.    if (strcmp(plainPath, "\\") != 0) {
  69.       p = plainPath;  /* Initialize search for backslashes */
  70.       while ((p = strchr(++p, '\\')) != NULL) {
  71.          *p = 0;      /* Cut off string at backslash */
  72.          fprintf(bf, "tdrf md %s\n", plainPath);
  73.          *p = '\\';   /* Replace backslash */
  74.       }
  75.       fprintf(bf, "tdrf md %s\n", plainPath);
  76.       fprintf(bf, "tdrf del %s%s\n", plainPath, "\\*.*");
  77.       fprintf(bf, "tdrf t %s%s%s\n", plainPath, "\\*.* ", plainPath);
  78.    }
  79. } /* preparePath */
  80.  
  81.  
  82.  
  83.